import { Command } from '../constant';
import Taro, { getCurrentInstance } from '@tarojs/taro';
import { Dispatch } from 'typings';
import { IAllReducerProps } from '../types';
import { getReducerData } from '@/redux/store';
import { extraPathsValue } from '@/redux/util';
import api from 'api';

export default (dispatch: Dispatch) => {
  let action = {
    commonChange(...param: any) {
      dispatch({
        type: Command.commonChange,
        payload: extraPathsValue(...arguments),
      });
    },
    async page() {
      let itemStr = Taro.getStorageSync('b2b-wechat@login');
      if (itemStr) {
        this.findDetailMsg();
      }
    },
    async findDetailMsg() {
      let { user } = Taro.getStorageSync('b2b-wechat@login');
      action.commonChange('main.isLoading', true);
      let type = getCurrentInstance().router.params.type;
      let { pageNum, findMessageList } = getData().main;
      const params = {
        pageNum,
        userId: user?.id || '',
        type: type
      };
      let res = await api.messageController.findDetailMsg(params);
      if (res.code == '200') {
        this.commonChange('main.totalPage', res.data.totalPage);
        if (pageNum == 0) {
          action.commonChange('main.findMessageList', res.data.rows)
        } else {
          let newList = findMessageList.concat(
            res.data.rows,
          );
          action.commonChange('main.findMessageList', newList)
        }
      }
      this.commonChange('main.isLoading', false);
    },
  };
  return action;
};

function getData(): IAllReducerProps {
  return {
    main: getReducerData('packageAMessageListMain'),
  };
}

//create by moon https://github.com/creasy2010/moon
